from common.email_helper import email_send_with_appendix_list
from common.logger_helper import g_wlogger
from common.pandas_helper import PandasHelper
from k3_data_report.business.ddycbj_db import Ddycbj

__author__ = "Ypf"
_date_ = "2019/8/15-13:40"
from k3_data_report.business.cgtj_db import Cgtj
from k3_data_report.business.config import bussiness_cal_fields



def ddycbj(time1):
    db_sql = bussiness_cal_fields['k3_caozuo']
    check_sql = db_sql["insert_check_sql"] % ('k3ddycbj', time1)
    check_dic = Cgtj.check_cf(check_sql)
    get_180_sql = db_sql["ddyc_180_sql"]
    insert_sql = db_sql["insert_ddyc"]
    dhzq=180
    if len(check_dic) == 0:
        dic_180 = Ddycbj.get_dic(get_180_sql)
        Ddycbj.insert(insert_sql, dic_180, time1, dhzq)
    else:
        g_wlogger.winfo(f"ddycbj-{time1}:target_full_df, all data is empty.")
def create_df_ddycbj(time1=None):
    db_sql = bussiness_cal_fields['k3_caozuo']
    get_180_sql = db_sql["ddyc_180_sql"]
    dic_email = Ddycbj.get_dic(get_180_sql)
    dhzq=180
    df_data_list=[]
    # khmc,khjl,dhxh,tcdhsl,
    for key in dic_email:
        dic_email[key].extend([dhzq,'',''])
        end_tuple = tuple(dic_email[key])
        df_data_list.append(end_tuple)
    columns = ["客户名称", "客户经理", "订货型号", "通常订货数量", "通常订货周期（每周或每月、每几个月等）", "客户经理回复未接单的原因", "备注"]
    k3ddyc_df = PandasHelper.create_dataframe(data=df_data_list, columns=columns)
    return k3ddyc_df
def ddycbj_main(month):
    try:
        ddycbj(month)
        return True
    except Exception as e:
        g_wlogger.werror(f"ddycbj_main has error:{e} with:{month}")
        return False

if __name__ == "__main__":
    time1 = "2019-09"
    ddycbj(time1)
